import time

from flask import Blueprint
from .models import User, Resume, Operation, InterResult
from ibm import db, render_template, redirect, request, url_for, session, \
    flash, secure_filename, app, ALLOWED_EXTENSIONS, os
from sqlalchemy import and_, not_, or_

interview = Blueprint('interview', __name__)


@interview.route('/upload', methods=['GET'])
def upload():
    reviews = User.query.filter(User.role == 'REVIEW').all()
    return render_template('interview.html', reviews=reviews)


@interview.route('/upload', methods=['POST'])
def upload_post():
    resume_name = request.form['resume_name']
    resume_tel = request.form['resume_tel']
    age = request.form['resume_age']
    sex = request.form['sex']
    resume_edu = request.form['resume_edu']
    gradu_time = request.form['gradu_time']
    gradu_school = request.form['gradu_school']
    major = request.form['major']
    tech = request.form['tech']
    language = request.form['language']
    now_com = request.form['now_com']
    resume_from = request.form['resume_from']
    resume_to = request.form['resume_to']
    comment = request.form['comment']
    if 'resume_file' not in request.files:
        flash('No file')
        return redirect(request.url)
    file = request.files['resume_file']
    # If the user does not select a file, the browser submits an
    # empty file without a filename.
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if resume_name is '' or resume_tel is '' or age is '':
        flash('请填写必填项')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file_url = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        resume = Resume(None, resume_name, age, sex, resume_tel, \
                        resume_edu, gradu_time, gradu_school, major, tech, \
                        language, now_com, resume_from, file_url, '等待Review')
        db.session.add(resume)
        db.session.commit()
        re_id = Resume.query.filter(and_(Resume.tel == resume.tel, Resume.name == resume.name)).first().resume_id
        if Operation.query.filter(Operation.resume_id == re_id).first():
            flash('该简历已被添加')
            return redirect(request.url)
        add_man = session['user_id']
        # add_time =  time.strftime("%Y-%m-%d %H-%M-%S")
        operation = Operation(re_id, add_man, resume_to, comment, add_time=None)
        db.session.add(operation)
        db.session.commit()
        file.save(file_url)
        return '添加简历成功'


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@interview.route('/review', methods=['GET'])
def review():
    resume_id = request.args.get('resume_id')
    if resume_id is not None:
        resume = Resume.query.filter(Resume.resume_id == resume_id).first()
        session['resume_id'] = resume_id
        return render_template('interview_review.html', resume=resume)
    else:
        return redirect(request.url)


@interview.route('/review', methods=['POST'])
def review_post():
    # 获取前端传的review结果和评论
    # session中查传来的简历ID 并找到对应的Opreation对象
    # 更改review_result 和 review_comment
    resume_id = session['resume_id']
    review_man = session['user_id']
    review_result = request.form.get('review_result')
    review_comment = request.form.get('review_comment')
    Operation.query.filter(and_(Operation.resume_id == resume_id,
                                            Operation.review_man == review_man)).update(
        {'review_result':review_result,'review_comment':review_comment,'review_time':time.strftime("%Y-%m-%d %H-%M-%S")})
    Resume.query.filter(Resume.resume_id == resume_id).update({'status':review_result})
    db.session.commit()
    return 'Review成功'


@interview.route('/update')
def update():
    return render_template('interview_update.html')


@interview.route('/feedback', methods=['GET'])
def feedback():
    # 从对应的表中获取信息传给前端
    return render_template('interview_feedback.html')


@interview.route('/feedback', methods=['POST'])
def feedback_post():
    # 把面试反馈写到 inter_result里面
    return '反馈成功'


@interview.route('/manager', methods=['GET'])
def manager():
    user_id = session['user_id']
    # resumes = db.session.query(Resume).filter(and_(Resume.resume_id == Operation.resume_id,
    #                                     Operation.review_man == user_id,
    #                                     and_(Operation.review_result == None))).all()
    # 注意:这个==None 不能用 is
    resumes = db.session.query(Resume).filter(and_(Resume.resume_id == Operation.resume_id,
                                        Operation.review_man == user_id)).all()
    user = User.query.filter(User.user_id == user_id).first()
    return render_template('interview_management.html', resumes=resumes, user=user)
